package com.tcxhb.job.admin.controller;

import com.tcxhb.job.admin.convert.JobInfoConvert;
import com.tcxhb.job.dao.dataobject.query.JobInfoQuery;
import com.tcxhb.job.admin.model.request.biz.JobRunReq;
import com.tcxhb.job.admin.utils.ContextHolder;
import com.tcxhb.job.core.entity.JobRunDTO;
import com.tcxhb.job.dao.dataobject.JobInfoDO;
import com.tcxhb.job.admin.model.request.biz.JobInfoAddReq;
import com.tcxhb.job.admin.model.request.UpdateStatusReq;
import com.tcxhb.job.admin.model.request.query.JobInfoQueryReq;
import com.tcxhb.job.admin.model.response.JobInfoVO;
import com.tcxhb.job.core.schedule.execute.JobRunExecutor;
import com.tcxhb.job.core.service.job.JobInfoService;
import com.tcxhb.job.common.constants.CommonErrorCode;
import com.tcxhb.job.common.constants.enums.JobStatusEnum;
import com.tcxhb.job.common.model.MiscResult;
import com.tcxhb.job.common.model.PageResponse;
import com.tcxhb.job.common.request.CommonIdDeleteReq;
import com.tcxhb.job.common.request.CommonIdQueryReq;
import com.tcxhb.job.common.utils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * author:auto.generator
 * time: 2023-11-02
 */
@RestController
@RequestMapping("/job/jobInfo")
public class JobInfoController extends BaseController {

    @Autowired
    private JobInfoConvert convert;
    @Autowired
    private JobInfoService jobInfoService;
    @Autowired
    private JobRunExecutor executor;


    @PostMapping(value = "/page", consumes = MediaType.APPLICATION_JSON_VALUE)
    public MiscResult<PageResponse<JobInfoVO>> page(@RequestBody JobInfoQueryReq req) {
        fillAppName(req);
        JobInfoQuery query = convert.toQuery(req);
        PageResponse<JobInfoDO> response = jobInfoService.page(query);
        PageResponse<JobInfoVO> result = ConvertUtils.page(response, convert::do2VO);
        return MiscResult.suc(result);
    }

    @PostMapping(value = "/create", consumes = MediaType.APPLICATION_JSON_VALUE)
    public MiscResult<Boolean> create(@RequestBody JobInfoAddReq req) {
        req.check();
        JobInfoDO jobDO = convert.req2DO(req);
        jobDO.setStatus(JobStatusEnum.init.getType());
        Long id = jobInfoService.create(jobDO);
        return MiscResult.suc(true);
    }

    @PostMapping(value = "/queryById", consumes = MediaType.APPLICATION_JSON_VALUE)
    public MiscResult<JobInfoVO> queryById(@RequestBody CommonIdQueryReq req) {
        JobInfoDO relationDO = jobInfoService.queryById(req.getId());
        JobInfoVO vo = convert.do2VO(relationDO);
        return MiscResult.suc(vo);
    }

    @PostMapping(value = "/update", consumes = MediaType.APPLICATION_JSON_VALUE)
    public MiscResult<String> update(@RequestBody JobInfoAddReq req) {
        req.check();
        JobInfoDO update = convert.req2DO(req);
        update.setStatus(JobStatusEnum.init.getType());
        Boolean result = jobInfoService.updateById(update);
        if (Boolean.TRUE.equals(result)) {
            return MiscResult.suc("操作成功");
        }
        return MiscResult.err(CommonErrorCode.BAD_REQUEST);
    }

    @PostMapping(value = "/deleteById", consumes = MediaType.APPLICATION_JSON_VALUE)
    public MiscResult<String> deleteById(@RequestBody CommonIdDeleteReq req) {
        Boolean result = jobInfoService.deleteById(req.getId());
        if (Boolean.TRUE.equals(result)) {
            return MiscResult.suc("操作成功");
        }
        return MiscResult.err(CommonErrorCode.BAD_REQUEST);
    }

    @PostMapping(value = "/updateStatus", consumes = MediaType.APPLICATION_JSON_VALUE)
    public MiscResult<String> updateStatus(@RequestBody UpdateStatusReq req) {
        req.check();
        JobInfoDO jobInfo = new JobInfoDO();
        jobInfo.setStatus(req.getStatus());
        jobInfo.setId(req.getId());
        Boolean result = jobInfoService.updateById(jobInfo);
        if (Boolean.TRUE.equals(result)) {
            return MiscResult.suc("操作成功");
        }
        return MiscResult.err(CommonErrorCode.BAD_REQUEST);
    }

    @PostMapping(value = "/start", consumes = MediaType.APPLICATION_JSON_VALUE)
    public MiscResult<String> start(@RequestBody CommonIdQueryReq req) {
        Boolean result = jobInfoService.start(req.getId());
        if (Boolean.TRUE.equals(result)) {
            return MiscResult.suc("操作成功");
        }
        return MiscResult.err(CommonErrorCode.BAD_REQUEST);
    }

    @PostMapping(value = "/run", consumes = MediaType.APPLICATION_JSON_VALUE)
    public MiscResult<Boolean> run(@RequestBody JobRunReq req) {
        JobRunDTO runDTO = new JobRunDTO(req.getJobId(), System.currentTimeMillis());
        runDTO.setRunOnce(true);
        return executor.run(runDTO);
    }
}
